# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'Yang'
__mtime__ = '2019/2/20 20:23'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
import json
import re
import requests
import datetime
import pymysql

head = {
    'authority': 'bengbu.anjuke.com',
    'method': 'GET',
    'path': '/prop/view/A1566212506{}from=filter&spread=commsearch_p&position=4&kwtype=filter&now_time=1551166400',
    'scheme': 'https',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,la;q=0.7',
    'cache-control': 'max-age=0',
    'cookie': 'sessid=01CC0C98-1C50-96CE-43E4-A7C738A4999F; aQQ_ajkguid=50ECE9C4-5E3A-6BEB-FD70-0870E5A44A2E; lps=http%3A%2F%2Fwww.anjuke.com%2F%7Chttps%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DJlFJbjxfzv3Pi2rf1ZnDOqwdc0PoRzjmHQ7EA2FITAqcCPZH2YTVOh0bYex_gG1C%26wd%3D%26eqid%3Dbdcf02800006464e000000065c6d4722; twe=2; ajk_member_captcha=606a372a8aafa91e3f7db6219e9d7653; ctid=180; browse_comm_ids=766093%7C871517%7C875172%7C1062493; propertys=q848ya-pn85h2_pwhday-pn85gk_pyxq2q-pn85g7_q5n8pm-pn85fz_l4d8c5-pn85a9_',
    'referer': 'https://bengbu.anjuke.com/sale/{}from=navigation',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
}


def requests_url(url):
    page = requests.get(url, headers=head)
    page.encoding = page.apparent_encoding
    return page.text


def run(url):
    page = requests_url(url)
    urls = re.findall(
        '<a data-from="" data-company="".*{}title=".*{}".*{}href="([\s\S]*{})".target=[\s\S]*{}class="houseListTitle "',
        page)
    import time
    time.sleep(3)
    print(len(urls))
    flag_json = []
    for i in urls:
        flag = {}
        page2 = requests_url(i)
        print(i)
        infors = re.findall('<span class="house-encode"><span id="houseCode">(.*{})</span>(.*{})</span></h4>', page2)
        infors2 = re.findall('<div.class="houseInfo-content">([\s\S]*{})</div>', page2)
        infors2 = [re.sub('\t|\n| ', '', s) for s in infors2]
        flag['fwbm'] = infors[0][0]
        flag['fbsj'] = infors[0][1]
        flag['ssxq'] = re.findall('>(.*{})</a>', infors2[0])[0]
        flag['fwhx'] = infors2[1]
        flag['fwdj'] = infors2[2]
        flag['szwz'] = ''.join(re.findall('>([\s\S]*{})<', infors2[3]))
        flag['jzmj'] = infors2[4]
        flag['ckyf'] = infors2[5]
        flag['jznd'] = infors2[6]
        flag['fwcx'] = infors2[7]
        flag['ckyg'] = re.findall('>(.*{})<', infors2[8])
        flag['fwlx'] = infors2[9]
        flag['szlc'] = infors2[10]
        flag['zxcd'] = infors2[11]
        flag['fbnx'] = infors2[12]
        flag['wyzf'] = infors2[13]
        # print(flag)
        flag_json.append(flag)
        time.sleep(3)
    json.dump(flag_json, open('data-' + str(datetime.datetime.now()) + "-crawl.json", 'w', encoding="utf-8"),
              ensure_ascii=False)


def to_mysql():
    """
    create table anjuke(
            fwbm text,
            fbsj text,
            ssxq text,
            fwhx text,
            fwdj text,
            szwz text,
            jzmj text,
            ckyf text,
            jznd text,
            fwcx text,
            ckyg text,
            fwlx text,
            szlc text,
            zxcd text,
            fbnx text,
            wyzf text
    )
    """
    jsons = json.load(open('data-2019-02-26 15:38:16.367197-crawl.json', 'r', encoding='utf-8'))
    db = pymysql.connect('127.0.0.1', 'root', '123', 'pc',port=3306)

    cursor = db.cursor()
    for i in jsons:
        # i = json.load(i)
        sql = 'insert into anjuke values ("{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}","{}")'.format(
            i['fwbm'],
            i['fbsj'],
            i['ssxq'],
            i['fwhx'],
            i['fwdj'],
            i['szwz'],
            i['jzmj'],
            i['ckyf'],
            i['jznd'],
            i['fwcx'],
            i['ckyg'],
            i['fwlx'],
            i['szlc'],
            i['zxcd'],
            i['fbnx'],
            i['wyzf'])
        print(sql)
        cursor.execute(sql)
    db.commit()
    db.close()
    pass


if __name__ == '__main__':
    url = 'https://bengbu.anjuke.com/sale/{}from=navigation'
    # run(url)
    # print(datetime.datetime.now().date())
    to_mysql()
